using System;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;

using Server.Functions;

namespace Server.Conn
{
    public sealed class ConnMgr : Singleton<ConnMgr>
    {
        MySqlConnection m_db = null;
        bool m_connected = false;

        public ConnMgr()
        {
            try
            {
                m_db = new MySqlConnection(string.Format("Server={1};Port={2};Uid={3};Pwd={4};Database={0};Charset=utf8;", MySQL.DataBase, MySQL.Host, MySQL.Port, MySQL.UserName, MySQL.Password));
                m_db.Open();
            }
            catch (MySqlException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            if (m_db != null)
            {
                if (m_db.State != ConnectionState.Closed)
                {
                    this.m_connected = true;
                }
                else
                {
                    Console.WriteLine("SQL Connection error");
                }
            }
        }

        ~ConnMgr()
        {
            m_db.Close();
            m_db = null;
        }

        public bool Connect()
        {
            if (!m_connected)
            {
                if (m_db.State == ConnectionState.Open)
                {
                    m_connected = true;
                    return true;
                }
                try
                {
                    m_db.Open();
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                if (m_db != null)
                {
                    if (m_db.State != ConnectionState.Closed)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                return true;
            }
            return true;
        }

        public bool IsConnected()
        {
            return m_connected;
        }

        public int ExecuteNonQuery(string strsql)
        {
            MySqlCommand cmd = new MySqlCommand(strsql, m_db);
            int result = -1;
            try
            {
                result = cmd.ExecuteNonQuery();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return result;
        }

        public int ExecuteScaler(string strsql)
        {
            MySqlCommand cmd = new MySqlCommand(strsql, m_db);
            int result = 0;
            try
            {
                result = (int)cmd.ExecuteScalar();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return result;
        }

        public MySqlDataReader ExecuteReader(string strsql)
        {
            MySqlCommand cmd = new MySqlCommand(strsql, m_db);
            MySqlDataReader reader = null;
            try
            {
                reader = cmd.ExecuteReader();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return reader;
        }
    }
}